Skip to content

fix(parser): allow comments between a bare return and its terminator - #3

Merged
smn merged 1 commit into
mainfrom
fix-return-comment-terminator
Jul 28, 2026
Merged

fix(parser): allow comments between a bare return and its terminator#3
smn merged 1 commit into
mainfrom
fix-return-comment-terminator

Conversation

@smn

@smn smn commented Jul 28, 2026

Copy link
Copy Markdown

Problem

A valueless return followed by a comment before the block terminator (elseif / else / end / until) fails to parse:

if method == "GET" then
    -- ...
    return
-- Handle POST requests
elseif method == "POST" then
    -- ...
end
Parse Error at line ..., column 5: Expected expression

Real Lua 5.3 and Luerl both accept this — comments are whitespace, so the return is simply a bare (valueless) return and the branch continues. This is a common idiom (then return -- note \n elseif ...).

Why it matters for us

This VM runs tenant Lua in production (engage journey engine) and in the lua-dev SDK. The bug surfaced via make test-lua-app APP=healthpass, whose core/init.lua has exactly this shape — the whole module failed to load, reported downstream as a misleading attempt to concatenate a nil value.

Root cause

Lua.Parser.parse_return/1 peeked at the raw next token to decide whether the return was valueless. A comment token isn't one of the terminators, so it fell through to parse_expr_list, which then choked on the comment → "Expected expression".

Fix

Peek past comment tokens (skip_comments/1) when detecting the terminator. The comment tokens stay in the stream, so the block parser still collects them as orphaned/trailing comments (no comment metadata is lost).

case peek(skip_comments(rest)) do

Tests

  • New regression test in test/lua/parser/statement_test.exs covering return + comment before elseif/else/end — fails before, passes after.
  • mix test test/lua/parser/ — 346 passed
  • mix test (full suite) — 2638 passed, 7 skipped
  • Verified against the real-world file that surfaced this: all 76 healthpass .lua files now parse.

🤖 Generated with Claude Code

A valueless `return` followed by a comment before the block terminator
(`elseif`/`else`/`end`/`until`) failed to parse with "Expected expression".
`parse_return/1` peeked at the raw next token to detect an empty return; a
comment token is not a terminator, so it fell through to `parse_expr_list`
and choked on the comment.

Comments are whitespace in Lua, so peek past them (`skip_comments/1`) when
deciding whether the return is bare. The comment tokens are left in the
stream so the block parser still collects them as orphaned/trailing
comments. Real Lua 5.3 and Luerl both accept this; the idiom shows up in
`if ... then return -- note \n elseif ...` style branches.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@smn
smn requested a review from fedme July 28, 2026 08:21
@smn

smn commented Jul 28, 2026

Copy link
Copy Markdown
Author

Filed upstream as well in tv-labs#418

@smn
smn requested a review from santiagocardo July 28, 2026 08:25
@smn
smn merged commit 79415fe into main Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants